/*
 * @Author: song
 * @Description: timeline 轨道
 * @Date: 2024年04月28日 星期日 20:04:46
 * @Modify:
 */

using System;
using System.Collections.Generic;
using Function.SkillEditor.Runtime;
using SCore.Editor.WindowDraw;
using SCore.Function.Fixed;
using UnityEngine;

namespace Function.SkillEditor.Editor
{
    public class SelectTrack
    {
        public ISkillTrackDrawer _selectDrawer; //选择的轨道

        public ISkillClipDrawer _clipDrawer;

        public bool SelectDrawer(ISkillTrackDrawer drawer)
        {
            return _selectDrawer == drawer;
        }

        public bool SelectClip(ISkillClipDrawer clipDrawer)
        {
            if (_clipDrawer == null || clipDrawer == null)
            {
                return false;
            }

            return _clipDrawer == clipDrawer;
        }
    }

    public class SkillTimeLineTrackView : NCommonView
    {
        private SkillTimeLineMarkerView _markerView;
        private SkillTimeLineHeadView _headView;
        private SkillTimeLineCursorView _cursorView;
        private ELineView _eLineView;
        private static int TRACKHEIGHT = 40; //Track高度
        private static int TRACK_INV = 2; //Track间隔
        public SelectTrack _select;


        private Dictionary<SkillTrack, ISkillTrackDrawer> _drawSkillTrackMap =
            new Dictionary<SkillTrack, ISkillTrackDrawer>();

        private Dictionary<Type, TrackDrawEditorAttribute> _drawLayoutMap =
            new Dictionary<Type, TrackDrawEditorAttribute>();

        public void OnTrackGUI()
        {
            var timelineWindow = GetSubEditorWindow<SkillTimeLineSubWindow>();
            var skillTracks = timelineWindow._skillConfigItem.GetSkillTracks();
            for (int index = 0; index < skillTracks.Count; index++)
            {
                float offest = (TRACKHEIGHT + TRACK_INV) * index;
                Rect groupRect = new Rect(_rect.x, _rect.y + offest,
                    _rect.width * _eLineView.GetCurPercent(), TRACKHEIGHT);

                Rect trackRect = new Rect(groupRect.width, offest,
                    _rect.width * (1 - _eLineView.GetCurPercent()), TRACKHEIGHT);

                GUILayout.Space(TRACKHEIGHT + TRACK_INV);
                //创建绘制轨道类
                if (!_drawSkillTrackMap.TryGetValue(skillTracks[index], out var skillTrackDrawer))
                {
                    if (_drawLayoutMap.TryGetValue(skillTracks[index].GetType(), out var reg))
                    {
                        var regAttribute = GetSubEditorWindow<SkillTimeLineSubWindow>()
                            .GetRegAttribute(reg._trackType);
                        if (regAttribute != null)
                        {
                            skillTrackDrawer =
                                Activator.CreateInstance(reg._drawType,
                                    (object)skillTracks[index], regAttribute) as ISkillTrackDrawer;
                            regAttribute._clipDrawType = reg._clipDrawType;
                            _drawSkillTrackMap.Add(skillTracks[index], skillTrackDrawer);
                        }
                    }
                }

                if (skillTrackDrawer != null)
                {
                    skillTrackDrawer.SetUp(
                        timelineWindow._skillConfigItem._skillRunLine,
                        _markerView, this);
                    skillTrackDrawer.Bind(groupRect, trackRect);
                    skillTrackDrawer.OnGUI();
                    skillTrackDrawer.UpdateEvent(Event.current);
                }
            }
        }

        public void Bind(SkillTimeLineMarkerView markerView, ELineView eLineView, SkillTimeLineHeadView headView,
            SkillTimeLineCursorView cursorView)
        {
            _markerView = markerView;
            _eLineView = eLineView;
            _headView = headView;
            _cursorView = cursorView;
        }

        public override void OnEnable()
        {
            _drawSkillTrackMap.Clear();
            _drawLayoutMap.Clear();
            _select = new SelectTrack();
            SkillEditorWindow.EventManager.AddListener<fp>(SkillEditorEventType.TimeLinePlayFrameChange,
                OnTimeLinePlayFrameChange);
            SkillEditorWindow.EventManager.AddListener<SkillConfigItemData>(SkillEditorEventType.TimeLineEditChange,
                OnTimeLineEditChange);
            var regList = SkillTrackDrawerHelper.FindAllDrawEditors();
            foreach (var reg in regList)
            {
                _drawLayoutMap.Add(reg._trackType, reg);
            }
        }

        public override void OnDisable()
        {
            SkillEditorWindow.EventManager.RemoveListener<fp>(SkillEditorEventType.TimeLinePlayFrameChange,
                OnTimeLinePlayFrameChange);
            SkillEditorWindow.EventManager.RemoveListener<SkillConfigItemData>(SkillEditorEventType.TimeLineEditChange,
                OnTimeLineEditChange);
        }

        public override void Update()
        {
            if (_headView._play)
            {
                var time = _cursorView._curFrame * SkillRunMap.Instance.GetEOneFrameTime();
                //$"cur Time{time} curFrame {_cursorView._curFrame.AsInt()}".Error();
                foreach (var drawSkillTrack in _drawSkillTrackMap)
                {
                    drawSkillTrack.Value.OnProgress(true, time);
                }
            }
        }

        private void OnTimeLinePlayFrameChange(fp playerFrame)
        {
            var time = playerFrame * SkillRunMap.Instance.GetEOneFrameTime();
            //$"cur Time{time} curFrame {playerFrame}".Error();
            foreach (var drawSkillTrack in _drawSkillTrackMap)
            {
                drawSkillTrack.Value.OnProgress(false, time);
            }
        }

        private void OnTimeLineEditChange(SkillConfigItemData skillConfigItemData)
        {
            _drawSkillTrackMap.Clear();
        }
    }
}